package com.ruoyi.ftrl.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.ftrl.domain.EquipmentDocking;
import com.ruoyi.ftrl.domain.EquipmentInfo;
import com.ruoyi.ftrl.domain.RoomInfo;
import com.ruoyi.ftrl.mapper.EquipmentDockingMapper;
import com.ruoyi.ftrl.mapper.EquipmentInfoMapper;
import com.ruoyi.ftrl.mapper.RoomInfoMapper;
import com.ruoyi.ftrl.service.IEquipmentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 设备管理Service业务层处理
 * 
 * @author dihang
 * @date 2024-07-08
 */
@Service
public class EquipmentInfoServiceImpl implements IEquipmentInfoService 
{
    @Autowired
    private EquipmentInfoMapper equipmentInfoMapper;
    @Autowired
    private EquipmentDockingMapper equipmentDockingMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;

    /**
     * 查询设备管理
     * 
     * @param id 设备管理主键
     * @return 设备管理
     */
    @Override
    public EquipmentInfo selectEquipmentInfoById(Long id)
    {
        return equipmentInfoMapper.selectEquipmentInfoById(id);
    }

    /**
     * 查询设备管理列表
     * 
     * @param equipmentInfo 设备管理
     * @return 设备管理
     */
    @Override
    public List<EquipmentInfo> selectEquipmentInfoList(EquipmentInfo equipmentInfo)
    {
        List<EquipmentInfo> equipmentInfos = equipmentInfoMapper.selectEquipmentInfoList(equipmentInfo);
        for (EquipmentInfo info : equipmentInfos) {
           EquipmentDocking equipmentDocking= equipmentDockingMapper.selectEquipmentDockingByNodeId(info.getNodeId());
           if (equipmentDocking!=null){
               info.setEquipmentDocking(equipmentDocking);
           }
        }
        return equipmentInfos;

    }

    /**
     * 新增设备管理
     * 
     * @param equipmentInfo 设备管理
     * @return 结果
     */
    @Override
    public int insertEquipmentInfo(EquipmentInfo equipmentInfo)
    {
        equipmentInfo.setCreateTime(DateUtils.getNowDate());
        equipmentInfo.setCreateBy(SecurityUtils.getUsername());
        //新增设备时将设备绑定的房间编号带入
        RoomInfo roomInfo=new RoomInfo();
        roomInfo.setCommunityName(equipmentInfo.getCommunityName());
        roomInfo.setBuildingName(equipmentInfo.getBuildingName());
        roomInfo.setUnitName(equipmentInfo.getUnitName());
        roomInfo.setHouseNumber(equipmentInfo.getRoomAme());
        List<RoomInfo> roomInfos = roomInfoMapper.selectRoomInfoList(roomInfo);
        //如果设备绑定房间不存在，抛出异常 //todo
        RoomInfo roomInfo1 = roomInfos.get(0);
        if (roomInfo1==null){
             throw new ServiceException("您所绑定该设备对应的房间不存在，请先添加后再进行绑定！");
        }
        equipmentInfo.setRoomNo(roomInfo1.getRoomNo());
        //户主姓名
        equipmentInfo.setUserName(roomInfo1.getPersonName());
        return equipmentInfoMapper.insertEquipmentInfo(equipmentInfo);
    }

    /**
     * 修改设备管理
     * 
     * @param equipmentInfo 设备管理
     * @return 结果
     */
    @Override
    public int updateEquipmentInfo(EquipmentInfo equipmentInfo)
    {
        equipmentInfo.setUpdateTime(DateUtils.getNowDate());
        equipmentInfo.setUpdateBy(SecurityUtils.getUsername());
        return equipmentInfoMapper.updateEquipmentInfo(equipmentInfo);
    }   

    /**
     * 批量删除设备管理
     * 
     * @param ids 需要删除的设备管理主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentInfoByIds(Long[] ids)
    {
        return equipmentInfoMapper.deleteEquipmentInfoByIds(ids);
    }

    /**
     * 删除设备管理信息
     * 
     * @param id 设备管理主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentInfoById(Long id)
    {
        return equipmentInfoMapper.deleteEquipmentInfoById(id);
    }
}
